﻿Public Class frmproductlist
    Dim Strsql As String
    Dim Mysql As New ClsSQLhelper
    Dim On_load As Boolean = True
#Region "Function"
    Sub Load_Data(Optional ByVal sectionid As Integer = 0)
        Strsql = "select itemnumber,item.itemname,alternatenumber,uom,section.section_name as section,category.Category_name as category,registerdate,item.active,item.note  from item  "
        Strsql = Strsql & " left join section on item.section= section.section_id "
        Strsql = Strsql & " left join category on  item.category = category.Category_id "
        If sectionid <> 0 Then
            Strsql = Strsql & " where section = " & Me.Cbosection.SelectedValue
        End If
        Strsql = Strsql & " order by section.section_name,category.Category_name,itemname"

        Dim DS As New DataSet
        DS = Mysql.GetMYSQLDataset(Strsql, "DSItem", "item")
        With GridProductsList
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .ReadOnly = True
            .DataSource = DS.Tables("item")
            .Columns("itemnumber").HeaderText = "รหัส"
            .Columns("itemnumber").Width = 50
            .Columns("itemnumber").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns("itemname").HeaderText = "ชื่อวัสดุ"
            .Columns("itemname").Width = 180
            .Columns("alternatenumber").HeaderText = "Barcode"
            .Columns("alternatenumber").Width = 80
            .Columns("uom").HeaderText = "หน่วยนับ"
            .Columns("uom").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns("section").HeaderText = "กลุ่มงาน"
            .Columns("section").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns("category").HeaderText = "ประเภท"
            .Columns("category").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns("note").HeaderText = "หมายเหตุ"
            .Columns("registerdate").HeaderText = "วันที่ลงทะเบียน"
            .Columns("registerdate").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns("registerdate").Width = 150
            .Columns("registerdate").DefaultCellStyle.Format = "D"
            .Columns("active").HeaderText = "เปิดใช้งาน"
        End With

    End Sub
    Sub Load_ProductGroup()
        Strsql = "Select section_id,section_name from section order by readonly desc,section_name"
        Dim DTSection As New DataTable
        DTSection = mysql.GetMYSQLDataTable(Strsql, "section")
        With Me.Cbosection
            .DataSource = DTSection
            .DisplayMember = "section_name"
            .ValueMember = "section_id"
        End With
    End Sub
#End Region
    Private Sub frmproductlist_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Data()
        Load_ProductGroup()
        On_load = False
    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)

    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub

    Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNew.Click
        With frmproductdetail
            .NewEntry_State = True
            .ShowDialog()
        End With

    End Sub

    Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        With frmproductdetail
            .NewEntry_State = False
            .TxtItemNumber.Text = Me.GridProductsList.Rows(GridProductsList.CurrentCell.RowIndex).Cells(0).Value
            .ShowDialog()
        End With

    End Sub

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    End Sub

    Private Sub GridProductsList_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridProductsList.CellContentClick

    End Sub

    Private Sub GridProductsList_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridProductsList.CellDoubleClick
        With frmproductdetail
            .NewEntry_State = False
            .TxtItemNumber.Text = Me.GridProductsList.Rows(GridProductsList.CurrentCell.RowIndex).Cells(0).Value
            .ShowDialog()
        End With
    End Sub

    Private Sub Cbosection_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cbosection.SelectedIndexChanged
        If On_load = True Then Exit Sub
        Load_Data(Cbosection.SelectedValue)
    End Sub
End Class